package com.elm_springboot_s;


import java.util.ArrayList;
import java.util.List;
// Component.java
 interface Component {
    void render();
}
// Button.java
 class Button implements Component {
    @Override
    public void render() {
        System.out.println("Rendering Button");
    }
}

// TextField.java
 class TextField implements Component {
    @Override
    public void render() {
        System.out.println("Rendering TextField");
    }
}


// Composite.java
 class Composite implements Component {
    private List<Component> children = new ArrayList<>();

    // 添加子组件
    public void add(Component component) {
        children.add(component);
    }

    // 移除子组件
    public void remove(Component component) {
        children.remove(component);
    }

    @Override
    public void render() {
        for (Component child : children) {
            child.render();  // 渲染所有子组件
        }
    }
}
// UIFactory.java
 interface UIFactory {
    Button createButton();
    TextField createTextField();
}
// WindowsFactory.java
 class WindowsFactory implements UIFactory {
    @Override
    public Button createButton() {
        return new Button();  // 创建 Windows 风格的按钮
    }

    @Override
    public TextField createTextField() {
        return new TextField();  // 创建 Windows 风格的文本框
    }
}

// MacFactory.java
 class MacFactory implements UIFactory {
    @Override
    public Button createButton() {
        return new Button();  // 创建 Mac 风格的按钮
    }

    @Override
    public TextField createTextField() {
        return new TextField();  // 创建 Mac 风格的文本框
    }
}
public class Client {
    public static void main(String[] args) {
        // 创建 Windows 风格的工厂
        UIFactory factory = new WindowsFactory();

        // 创建 UI 组件
        Button button1 = factory.createButton();
        TextField textField1 = factory.createTextField();

        // 创建容器，将组件添加到容器中
        Composite composite = new Composite();
        composite.add(button1);
        composite.add(textField1);

        // 渲染组件（包括容器中的所有子组件）
        composite.render();

        // 创建 Mac 风格的工厂
        UIFactory macFactory = new MacFactory();

        // 创建 Mac 风格的组件
        Button button2 = macFactory.createButton();
        TextField textField2 = macFactory.createTextField();

        // 创建容器，将组件添加到容器中
        Composite macComposite = new Composite();
        macComposite.add(button2);
        macComposite.add(textField2);

        // 渲染 Mac 风格的组件
        macComposite.render();
    }
}
